//
//  RootViewController.m
//  iTour4China
//
//  Created by Wang Jun on 11-8-4.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//
#import "RootViewController.h"
#import "Three20/Three20.h"
#import "ItemListController.h"

@implementation RootViewController

#pragma mark -
#pragma mark View lifecycle

@synthesize title_view,content_view;

- (void)viewDidLoad {
	[self.navigationController setNavigationBarHidden:YES animated:NO];
	[super loadView];
	
   	int total        = 9;
	int columeCount  = 3;
	int countPerPage = 9;
	
	int pagesCount = (total / countPerPage) + 1;
	
	NSArray *itemTitles = [[NSArray alloc] initWithObjects:@"海岛旅行",@"浪漫之旅",@"环球旅行",@"阳光之旅"\
						   ,@"蓝天旅行",@"红色之旅",@"自由行",@"音乐之旅",@"生日之旅",nil];
	NSMutableArray *pages = [[NSMutableArray alloc] initWithCapacity:pagesCount];
	//	for (int i = 0;  i < pagesCount ; i++) {
	//		NSMutableArray *page = [[NSMutableArray alloc] init];
	//		int current = total - ;
	//		for (int j = 0 ; j < ; <#increment#>) {
	//			<#statements#>
	//		}
	//	}
	NSMutableArray *page = [[NSMutableArray alloc] init];
	for (int i = 0 ;  i < 9 ; i++ ) {
		NSString *imageStr = [[NSString alloc] initWithFormat:@"bundle://%@.png",@"Icon",nil];
		NSString *url = [[NSString alloc] initWithFormat:@"iTour://item%d",i,nil];
		TTLauncherItem *item = [[[TTLauncherItem alloc] initWithTitle:[itemTitles objectAtIndex:i] 
																image:imageStr URL:url canDelete:NO] autorelease];
		[page addObject:item];
		[imageStr release];
		[url release];
	}
	[pages addObject:page];
	[page release];
	[itemTitles release];
	
	_launcherView = [[TTLauncherView alloc] initWithFrame:self.content_view.bounds];
	_launcherView.backgroundColor = [UIColor clearColor];
	_launcherView.delegate = self;
	_launcherView.columnCount = 3;
	_launcherView.pages = pages;
	
   [self.content_view addSubview:_launcherView];
    self.title_view.backgroundColor = [UIColor clearColor];
	self.content_view.backgroundColor = [UIColor clearColor];
	//
	//	TTLauncherItem* item = [_launcherView itemWithURL:@"fb://item3"];
	//	item.badgeNumber = 4;
	
}



- (void)viewWillAppear:(BOOL)animated {
	[self.navigationController setNavigationBarHidden:YES animated:YES];
    [super viewWillAppear:animated];
}


- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}
 

- (void)viewWillDisappear:(BOOL)animated {
 [super viewWillDisappear:animated];
 }

- (void)viewDidDisappear:(BOOL)animated {
 [super viewDidDisappear:animated];
}
 

///////////////////////////////////////////////////////////////////////////////////////////////////
// TTLauncherViewDelegate

- (void)launcherView:(TTLauncherView*)launcher didSelectItem:(TTLauncherItem*)item {
	NSIndexPath *indexPath = [_launcherView indexPathOfItem:item];
	
	NSLog(@"row:%d",[indexPath row]);
	NSLog(@"selection:%d",[indexPath section]);
	
	ItemListController *itemListController = [[ItemListController alloc] initWithNibName:@"ItemListController" 
																				  bundle:[NSBundle mainBundle]];
	[itemListController setTitle:[item title]];
	[self.navigationController pushViewController:itemListController animated:YES];
	UIBarButtonItem *backItem = [[UIBarButtonItem alloc] init];  
	backItem.title = @"返回";
	[self.navigationItem setBackBarButtonItem: backItem]; 
    [itemListController release];
	
	
}

- (void)launcherViewDidBeginEditing:(TTLauncherView*)launcher {
	//[self.navigationItem setRightBarButtonItem:[[[UIBarButtonItem alloc]
	//											 initWithBarButtonSystemItem:UIBarButtonSystemItemDone
	//											 target:_launcherView action:@selector(endEditing)] autorelease] animated:YES];
}

- (void)launcherViewDidEndEditing:(TTLauncherView*)launcher {
	//[self.navigationItem setRightBarButtonItem:nil animated:YES];
}





#pragma mark -
#pragma mark Memory management

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Relinquish ownership any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
    // For example: self.myOutlet = nil;
}


- (void)dealloc {
	[_launcherView release];
	[title_view release];
	[content_view release];
    [super dealloc];
}

@end

